package com.MSofLectures.Entity.Lecture;

import java.util.Iterator;
import java.util.List;
import java.util.Set;

import com.MSofLectures.DAO.Lecture_keyName;
import com.MSofLectures.DAO.Lecture_keyValue;
import com.MSofLectures.Entity.Basic;

public class Lecture extends Basic implements LectureImp {
	private static final long serialVersionUID = 1L;
	/**
	 * @Title Lecture.java
	 * @Package com.Entity.Lecture
	 * @author ufo
	 */
	private String lectureDescription;
	private String lectureId;
	private String lecture_submittorId;

	public Lecture(String lectureId, String lectureDescription,
			String lecture_submittorId) {
		this.lectureId = lectureId;
		this.lectureDescription = lectureDescription;
		this.lecture_submittorId = lecture_submittorId;
	}

	public Lecture(String lectureId) {
		this.lectureId = lectureId;
	}

	@Override
	public void save() {
		// TODO 自动生成的方法存根
		if (0 < keyValueMap.size()) {
			Set<String> set = keyValueMap.keySet();

			try {
				for (Iterator<String> i = set.iterator(); i.hasNext();) {
					String keyName = i.next();
					List<?> list1 = this.getDatabaseControler().hibernateSess
							.createQuery(
									"select lecture_keyNameId from Lecture_keyName where lecture_keyName = :lecture_keyName")
							.setString("lecture_keyName", keyName).list();
					Iterator<?> i1 = list1.iterator();
					saveKeyValue(i1.next().toString(), keyValueMap.get(keyName));
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public void saveKeyValue(String keyNameId, String[] target) {
		Lecture_keyValue keyValue = null;
		if (null == target[1]) {
			keyValue = new Lecture_keyValue();
		} else {
			keyValue = (Lecture_keyValue) this.getDatabaseControler().hibernateSess.load(
					Lecture_keyValue.class, target[1]);
		}
		keyValue.setLecture_keyValue(target[0]);
		keyValue.setLecture_keyNameId(keyNameId);
		keyValue.setLectureId(this.lectureId);
		this.getDatabaseControler().hibernateSess.save(keyValue);
		this.getDatabaseControler().commit();
	}

	private String mapKeyName(String keyNameId) {
		Lecture_keyName keyName = (Lecture_keyName) this.getDatabaseControler().hibernateSess
				.load(Lecture_keyName.class, keyNameId);
		return keyName.getLecture_keyName();
	}

	@Override
	public boolean map() {
		// TODO 自动生成的方法存根
		try {
			List<?> list = this.getDatabaseControler().hibernateSess
					.createQuery(
							"from Lecture_keyValue where lectureId= :lectureId")
					.setParameter("lectureId", this.lectureId).list();
			Iterator<?> i = list.iterator();
			while (i.hasNext()) {
				Lecture_keyValue keyValue = (Lecture_keyValue) i.next();
				String[] tmp = { keyValue.getLecture_keyValue(),
						keyValue.getLecture_keyValueId() };
				keyValueMap.put(mapKeyName(keyValue.getLecture_keyNameId()),
						tmp);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	@Override
	public void delete() {
		com.MSofLectures.DAO.Lecture lecture1 = (com.MSofLectures.DAO.Lecture) this.getDatabaseControler().hibernateSess
				.load(com.MSofLectures.DAO.Lecture.class, this.lectureId);
		Iterator<?> i= this.getDatabaseControler().hibernateSess
				.createQuery("from Lecture_keyValue where lectureId=:lectureId")
				.setString("lectureId", this.lectureId).list().iterator();
		this.getDatabaseControler().hibernateSess.delete(lecture1);
		while(i.hasNext())
			this.getDatabaseControler().hibernateSess.delete((com.MSofLectures.DAO.Lecture_keyValue)i.next());

		i=this.getDatabaseControler().hibernateSess
				.createQuery("from User_registerLecture where lectureId=:lectureId")
				.setString("lectureId", this.lectureId).list().iterator();
		while(i.hasNext())
			this.getDatabaseControler().hibernateSess.delete(i.next());
		
		i=this.getDatabaseControler().hibernateSess
				.createQuery("from Lecture_registerCode where lectureId=:lectureId")
				.setString("lectureId", this.lectureId).list().iterator();
		while(i.hasNext())
			this.getDatabaseControler().hibernateSess.delete(i.next());

		getDatabaseControler().commit();
	}

	@Override
	public String[] createRegisterCode(int number) {
		// TODO 自动生成的方法存根
		String[] target = new String[number];
		int i;
		for (i = 0; i < number; i++) {
			target[i] = com.MSofLectures.Tool.RandomStringCreater
					.createWithLettersAndNumbers(10);
			com.MSofLectures.DAO.Lecture_registerCode registerCode = new com.MSofLectures.DAO.Lecture_registerCode();
			registerCode.setLecture_registerCode(target[i]);
			registerCode.setLectureId(this.lectureId);
			registerCode.setLecture_registerCode_checked((byte) 0);
			this.getDatabaseControler().save(registerCode);
		}
		this.getDatabaseControler().commit();
		return target;
	}

	public String getLectureDescription() {
		return lectureDescription;
	}

	public void setLectureDescription(String lectureDescription) {
		this.lectureDescription = lectureDescription;
	}

	public String getLectureId() {
		return lectureId;
	}

	public void setLectureId(String lectureId) {
		this.lectureId = lectureId;
	}

	public String getLecture_submittorId() {
		return lecture_submittorId;
	}

	public void setLecture_submittorId(String lecture_submittorId) {
		this.lecture_submittorId = lecture_submittorId;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}
}